package com.fc.authentication.modules.order.service;

import com.fc.authentication.modules.order.domain.Order;
import com.fc.authentication.modules.order.domain.OrderItem;
import com.fc.authentication.modules.order.responsitory.OrderItemRespository;
import com.fc.authentication.modules.order.responsitory.OrderRespository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Sunny on 2018/6/30.
 */
@Service
public class OrderService {

    @Autowired
    private OrderRespository orderRespository;

    @Autowired
    private OrderItemRespository orderItemRespository;

    public Page<Order> findAllOrders(Pageable pageable)
    {
        Page<Order> orderPage = orderRespository.findAll(pageable);
        List<Order> orders = orderPage.getContent();

        for(Order order : orders)
        {
            List<OrderItem> orderItems = orderItemRespository.findByOrderId(order.getId());
            if (orderItems != null && !orderItems.isEmpty())
            {
                order.setOrderItems(orderItems);
            }
        }
        return orderPage;
    }
}
